package com.android.update.config;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import de.robv.android.xposed.XSharedPreferences;

public class HookConfig {
    public final String hookConfig = "hookDB";

    private static HookConfig hookDB;
    private boolean isXShare = false;

    public static HookConfig getIntance(Context context) {
        if (hookDB == null) {
            synchronized (HookConfig.class) {
                if (hookDB == null) {
                    if (context == null) {
                        hookDB = new HookConfig();
                    } else {
                        hookDB = new HookConfig(context);
                    }
                }
            }
        } else {
            hookDB.reload();
        }
        return hookDB;
    }

    public static HookConfig getIntance() {

        return getIntance(null);
    }

    private void reload() {
        if (isXShare) {
            ((XSharedPreferences) sharedPreferences).reload();
        }
    }

    private SharedPreferences sharedPreferences;

    @SuppressLint("WrongConstant")
    private HookConfig(Context context) {
        isXShare = false;
        sharedPreferences = context.getSharedPreferences(hookConfig, Activity.MODE_WORLD_READABLE);
    }

    private HookConfig() {
        isXShare = true;
        XSharedPreferences xSharedPreferences = new XSharedPreferences(Config.packageName, hookConfig);
        xSharedPreferences.makeWorldReadable();
        this.sharedPreferences = xSharedPreferences;
    }

    public void saveActivate(boolean activate) {
        sharedPreferences.edit().putBoolean("activate", activate).apply();
    }

    public boolean isActivate() {
        reload();
        return sharedPreferences.getBoolean("activate", true);
    }

    public void savePermissions(boolean permissions) {
        sharedPreferences.edit().putBoolean("permissions", permissions).apply();
    }

    public boolean isPermissions() {
        reload();
        return sharedPreferences.getBoolean("permissions", true);
    }

    public void saveTest(boolean permissions) {
        sharedPreferences.edit().putBoolean("test", permissions).apply();
    }

    public boolean isTest() {
        reload();
        return sharedPreferences.getBoolean("test", false);
    }

}
